package com.cq.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cq.health.common.MessageConst;
import com.cq.health.entity.PageResult;
import com.cq.health.entity.QueryPageBean;
import com.cq.health.entity.Result;
import com.cq.health.pojo.CheckItem;
import com.cq.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: health_project
 * @Package: com.cq.health.controller
 * @ClassName: CheckItemController
 * @Author: 悠久的苍穹
 * @Description: 检查项控制器
 * @Date: 2020/5/7 18:29
 * @Version: 1.0
 */
@RestController
@Slf4j
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    /**
     * 新增检查项web方法
     * @return
     */
    @RequestMapping("/add")
    //@RequestBody接收前端json数据
    public Result add(@RequestBody CheckItem checkItem){

        try {
            log.debug("CheckItemController checkItem:{}"+checkItem);
            checkItemService.add(checkItem);
            return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ADD_CHECKITEM_FAIL);
        }
    }

    /**
     * 分页查询检查项
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        try {
            log.debug("CheckItemController QueryPageBean:{}" + queryPageBean);
            PageResult pageResult = checkItemService.findByPage(queryPageBean);
            return pageResult;
        } catch (Exception e){
            e.printStackTrace();
        }
        // 失败，返回初始化结果，记录为0，数据为空
        return new PageResult(0L,null);
    }

    /**
     * 删除单个检查项
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public Result delete (Integer id){
        try {
            log.debug("CheckItemController id:" + id);
            checkItemService.delete(id);
            return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }
    }

    /**
     * 编辑检查项数据回显
     * @param id
     * @return
     */
    @RequestMapping("/updateUI")
    public Result updateUI(Integer id){
        try {
            log.debug("CheckItemController id:" + id);
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItem);
        }catch (Exception e){
             e.printStackTrace();
             return new Result(true,MessageConst.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 更新检查项
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody CheckItem checkItem){
        try {
            log.debug("CheckItemController checkItem:" + checkItem);
            checkItemService.update(checkItem);
            return new Result(true,MessageConst.EDIT_CHECKITEM_SUCCESS,checkItem);
        }catch (Exception e){
             e.printStackTrace();
            return new Result(false,MessageConst.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 查询全部检查项
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckItem> checkItemList = checkItemService.findAll();
            log.debug("--------------------------------checkItemList"+checkItemList);
            return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItemList);
        }catch (Exception e){
             e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL);
        }
    }
}
